package com.techfort.metalrss;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.techfort.metalrss.core.Feed;
import com.techfort.metalrss.db.FavouriteProvider;

public class Favourites extends Activity {

	private TextView title;
	private ListView list;
	private Dialog dialog;
	private Builder builder;
	private FavouriteProvider fp;
	private List<Feed> feeds = new ArrayList<Feed>();
	private FavouriteAdapter  adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		fp = new FavouriteProvider(this);
		fp.open();
		feeds = fp.getAll();
		
		title = (TextView) findViewById(R.id.title);
		title.setText("Your favourites. Click on item to view, long click to delete.");
		list = (ListView) findViewById(R.id.mainlist);
		adapter = new FavouriteAdapter(this, R.layout.fav_item, feeds);
		list.setAdapter(adapter);
		
	}
	
	
	private class FavouriteAdapter extends ArrayAdapter<Feed>{

		private List<Feed> items;
		
		public FavouriteAdapter(Context context, int textViewResourceId,
				List<Feed> objects) {
			super(context, textViewResourceId, objects);
			items = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View v = convertView;
			if(v==null){
				LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = li.inflate(R.layout.fav_item, null);
			}
			final Feed feed = items.get(position);
			
			TextView tv = (TextView) v.findViewById(R.id.textView1);
			tv.setText(feed.getFeed_name());
			
			View.OnLongClickListener cancelItem = new View.OnLongClickListener() {
				
				
				
				@Override
				public boolean onLongClick(View v) {
					builder = new AlertDialog.Builder(Favourites.this);
					builder.setMessage("Are you sure you want to delete this item?");
					builder.setNegativeButton("Cancel", new OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							if(dialog!=null){
								dialog.dismiss();
							}
						}
					});
					
					
					builder.setPositiveButton("Yes", new OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							fp.delete(feed);
							feeds.remove(feed);
							adapter.notifyDataSetChanged();
							dialog.dismiss();
						}
					});
					
					dialog = builder.create();
					dialog.show();
					return true;
				}
			};
			
			View.OnClickListener goToFeed = new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Favourites.this, ListFeeds.class);
					Bundle extras = new Bundle();
					
					extras.putParcelable("FEED", feed);
					intent.putExtras(extras);
					startActivity(intent);
				}
			};
			
			v.setOnClickListener(goToFeed);
			
			v.setOnLongClickListener(cancelItem);
			
			
			
			return v;
		}
		
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
    	MenuInflater mi = new MenuInflater(this);
    	mi.inflate(R.menu.simplemenu, menu);
    	MenuItem item = menu.getItem(0);
    	item.setTitle("Home");
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()){
    	case R.id.favourites:
    		Intent intent = new Intent(Favourites.this, Main.class);
    		startActivity(intent);
    	default:
    		return true;
    	}
    }
	
}
